import axios from 'axios'
axios.defaults.timeout = 20000
axios.defaults.headers['Content-Type'] = 'application/json'

if (process.env.NODE_ENV === 'development') { // 开发环境
    axios.defaults.baseURL = ''
} else { // 生产环境
    axios.defaults.baseURL = 'https://zhaq-sit-manager.bosafe.com/YqFkBosafe'
}

axios.interceptors.response.use(
response => {
    return response;
},
error => {
    return Promise.reject(error);
})

export default {
    fetchGet (url, params = {}) {
        let keys = Object.keys(params)
        keys.forEach((key, index) => {
            if (index == 0) {
                url += `?${key}=${params[key]}`
            } else {
                url += `&${key}=${params[key]}`
            }
        })
        return axios.get(url, params).then((res) => {
            return Promise.resolve(res)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    fetchPost (url, params = {}) {
        return axios.post(url, params).then((res) => {
            return Promise.resolve(res)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    fetchPostForm (url, params = {}, formData = new FormData()) {
        formData.append('json', JSON.stringify(params))
        return axios({
          url: url,
          method: 'POST',
          data: formData
        }).then((res) => {
          return Promise.resolve(res)
        }).catch((error) => {
          return Promise.reject(error)
        })
    },
    fetchPost2 (url, params = {}, formData = new FormData()) {
        formData.append('json', JSON.stringify(params))
        return axios({
            url: url,
            method: 'POST',
            data: formData
        }).then((res) => {
            return Promise.resolve(res)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    fetchPUT (url, params = {}, formData = new FormData()) {
        if (!formData.get('json')) {
            formData.append('json', JSON.stringify(params))
        } else {
            formData.delete('json')
            formData.append('json', JSON.stringify(params))
        }
        return axios({
            url: url,
            method: 'PUT',
            data: formData
        }).then((res) => {
            return Promise.resolve(res)
        }).catch((error) => {   
            return Promise.reject(error)
        })
    },
    fetchPostFormData (url, params = {}, formData = new FormData()) {
        if (!formData.get('json')) {
            formData.append('json', JSON.stringify(params))
        } else {
            formData.delete('json')
            formData.append('json', JSON.stringify(params))
        }
        return axios({
            url: url,
            method: 'POST',
            data: formData
        }).then((res) => {
            return Promise.resolve(res)
        }).catch((error) => {   
            return Promise.reject(error)
        })
    }
}

// function setAxiosHeader (verifyResult) {
//     axios.defaults.headers.common['verifyResult'] = verifyResult || ''
// }
